<!-- 这是会员的组件 -->
<template>
  <div>
    <van-loading v-if="isLoading" style="text-align: center; margin-top: 10px" type="spinner" color="#1989fa" />
    <div v-else>
      <span class="add_shop"><router-link to='/newsList'><van-icon name="arrow-left" />返回</router-link></span>
      <div class="container" v-if="newsData.length > 0">
        <div class="header_box" >
          <h4>{{newsData[0].title}}</h4>
          <p class="time_click">
            <span>发表时间：{{newsData[0].add_time}}</span>
            <span>点击次数：{{newsData[0].click}}</span>
          </p>
        </div>
        <div class="content" v-html="newsData[0].content"></div>

        <!-- 留言板 -->
        <div class="message_board">
          <h4 class="title">发表评论：</h4>
          <van-field
            v-model="message"
            rows="2"
            autosize
            type="textarea"
            maxlength="50"
            placeholder="请输入留言"
            show-word-limit
          />
          <van-button type="info" block @click="handlePushMessage">发表评论</van-button>
          <ul class="message_list">
            <li v-for="(item, index) in messageList" :key="index">
              <p>
                <span>第{{index + 1}}楼</span>
                <span>用户: {{item.user_name}}</span>
                <span>发表时间: {{item.add_time}}</span>
              </p>
              <p>内容：{{item.content}}</p>
            </li>
            <van-button type="danger" plain block @click="handleLoadMore">加载更多</van-button>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      newsData: [], // 新闻详情数据
      message: '', // 留言内容
      messageList: [], // 留言的内容列表
      nowPageId: 1, // 当前页面的id
      pageIndex: 1, // 当前第几页
      isLoading: true // 是否加载中
    }
  },
  methods: {
    // 获取新闻详情
    async getNewsDetail () {
      const { data } = await this.$http.get('/api/getnew/' + this.nowPageId)
      if (data.status !== 0) { console.log('接口请求失败') }
      this.newsData = data.message
      this.isLoading = false
    },
    // 将留言板内容放到数组中
    async handlePushMessage () {
      if (!this.message) {
        this.$toast('留言不能为空！')
        return false
      }
      this.handleAddComment()
      this.message = ''
    },
    // 获取评论信息
    async getCommentMessage () {
      const { data } = await this.$http.get('/api/getcomments/' + this.nowPageId + '?pageindex=' + this.pageIndex)
      if (data.status !== 0) { this.$toast('接口请求失败') }
      this.messageList = [...data.message, ...this.messageList]
    },
    // 加载更多
    handleLoadMore () {
      this.pageIndex = this.pageIndex + 1
      this.getCommentMessage()
    },
    // 添加评论
    async handleAddComment () {
      const { data } = await this.$http.post('/api/postcomment/' + this.nowPageId, {
        content: this.message
      })
      if (data.status !== 0) { this.$toast('评论提交失败') }
      this.getCommentMessage()
      this.$toast('提交成功')
    }
  },
  mounted () {
    this.nowPageId = this.$route.query.id
    // 获取新闻列表传递过来的新闻id
    this.getNewsDetail()
    // 获取当前新闻的评论
    this.getCommentMessage()
  }
}
</script>

<style scoped>

.add_shop a {
  position: fixed;
  top: 16px;
  left: 10px;
  color: #ffffff;
  font-size: 14px;
  display: flex;
  align-items: center;
}

.container {
  padding-bottom: 50px;
  padding-top: 50px;
}

.header_box {
  padding: 10px 10px;
  color: #1989fa;
  border-bottom: 1px solid #ccc;
}

.header_box h4 {
  text-align: center;
  font-size: 14px;
  margin: 0 0 8px 0;
}

.time_click {
  font-size: 12px;
  display: flex;
  justify-content: space-between;
  margin: 0;
}

.content {
  padding: 0 10px;
}

/* 留言板 */
.message_board {
  padding: 15px;
}

.title {
  font-weight: bold;
  box-shadow: 0 1px 1px #cccccc;
  padding-bottom: 20px;
}

.van-field {
  border: 1px solid #ccc;
  margin-bottom: 5px;
}

.message_list {
  margin-top: 5px;
}

.message_list li p {
  margin: 0;
  padding: 6px 4px;
  font-size: 12px;
}

.message_list li :first-child {
  background: #ccc;
}

.message_list li :first-child span {
  margin-right: 6px;
}
</style>
